package com.scqzy.shengchunqiang.sword2offer.day01;

import java.util.ArrayDeque;
import java.util.Deque;

/**
 * @Description: 剑指 Offer 09. 用两个栈实现队列
 * @Author 盛春强
 * @Date 2021/8/8 18:17
 */
public class CQueue {

    Deque<Integer> push;
    Deque<Integer> delete;

    public CQueue() {
        this.push = new ArrayDeque<>();
        this.delete = new ArrayDeque<>();
    }

    public void appendTail(int value) {
        push.push(value);
    }

    public int deleteHead() {
        if (delete.isEmpty()&&push.isEmpty()) {
            return -1;
        }
        if (delete.isEmpty()) {
            pushToDelete(push,delete);
        }
        return delete.pop();
    }

    private void pushToDelete(Deque<Integer> push, Deque<Integer> delete) {
        while (!push.isEmpty()) {
            delete.push(push.pop());
        }
    }

}
